//
//  MKMusicNameViewController.m
//  singForYou
//
//  Created by nicolas on 2017/3/8.
//  Copyright © 2017年 nicolas. All rights reserved.
//

#import "MKMusicNameViewController.h"
#import "MKMusicViewController.h"
#import "MKMusciModel.h"
#import <MJExtension/MJExtension.h>

@interface MKMusicNameViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSArray *musicList;

@end

@implementation MKMusicNameViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

#pragma mark - Delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.musicList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    MKMusciModel *musicModel = self.musicList[indexPath.row];
    cell.textLabel.text = musicModel.name;
    cell.detailTextLabel.text = musicModel.singer;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MKMusicViewController *musicVc = [[MKMusicViewController alloc] init];
    musicVc.musicIndex = indexPath.row;
    [self.navigationController pushViewController:musicVc animated:YES];
    
}
#pragma mark - Getter & Setter
-(NSArray *)musicList {
    if (_musicList == nil) {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"mlist.plist" withExtension:nil];
        NSArray *array = [NSArray arrayWithContentsOfURL:url];
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            MKMusciModel *model = [MKMusciModel mj_objectWithKeyValues:dict];
            [tempArray addObject:model];
        }
        _musicList = tempArray.copy;
    }
    return _musicList;
    
}

@end
